cmake_minimum_required(VERSION 3.10)
project(zk_demo)

set(CMAKE_CXX_STANDARD 14)

# 指定 libsnark 安装/源码目录（使用环境变量 HOME 替代 ~）
set(LIBSNARK_DIR "$ENV{HOME}/libsnark")

# 头文件路径
include_directories(
    ${CMAKE_SOURCE_DIR}
    ${LIBSNARK_DIR}
    ${LIBSNARK_DIR}/depends
    ${LIBSNARK_DIR}/depends/libff
    ${LIBSNARK_DIR}/depends/libfqfft
)

# 启用 BN128 曲线（如需要）
add_definitions(-DCURVE_BN128)

# 查找 GMP
find_library(GMP_LIB gmp)
find_library(GMPXX_LIB gmpxx)

# 你的 demo 源文件
add_executable(demo demo.cpp)

# 优先链接 libsnark 的静态库（如果存在），否则尝试使用库名
set(LIBSNARK_STATIC "${LIBSNARK_DIR}/build/libsnark/libsnark.a")
if(EXISTS ${LIBSNARK_STATIC})
    message(STATUS "Found libsnark archive: ${LIBSNARK_STATIC}")
    set(LIBFF_STATIC "${LIBSNARK_DIR}/build/depends/libff/libff/libff.a")
    set(LIBZM_STATIC "${LIBSNARK_DIR}/build/depends/libzm.a")
    if(EXISTS ${LIBFF_STATIC})
        message(STATUS "Found libff archive: ${LIBFF_STATIC}")
    endif()
    if(EXISTS ${LIBZM_STATIC})
        message(STATUS "Found libzm archive: ${LIBZM_STATIC}")
    endif()
    # Link order: demo -> libsnark -> libff -> libzm -> procps -> gmp
    find_library(PROCPS_LIB procps)
    if(PROCPS_LIB)
        message(STATUS "Found procps library: ${PROCPS_LIB}")
    else()
        message(WARNING "procps library not found; symbols used by libff profiling may be unresolved")
    endif()
    target_link_libraries(demo PRIVATE ${LIBSNARK_STATIC} ${LIBFF_STATIC} ${LIBZM_STATIC} ${PROCPS_LIB} ${GMP_LIB} ${GMPXX_LIB})
else()
    message(WARNING "libsnark archive not found at ${LIBSNARK_STATIC}; falling back to linking by name (may fail)")
    target_link_libraries(demo PRIVATE snark ff ${GMP_LIB} ${GMPXX_LIB})
endif()

# 如果需要额外的依赖静态库（例如 depends/libzm.a），可以在此处添加：
set(LIBSNARK_DEP_ZM "${LIBSNARK_DIR}/build/depends/libzm.a")
if(EXISTS ${LIBSNARK_DEP_ZM})
    target_link_libraries(demo PRIVATE ${LIBSNARK_DEP_ZM})
endif()
